﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Reviewer.Entities.Services;
using Reviewer.Entities;
using Reviewer.Business;

public partial class CMSV2_Controls_GameVideo : System.Web.UI.UserControl
{
    public Guid GameId
    {
        get
        {
            if (null == ViewState["GameId"])
            {
                return Guid.Empty;
            }

            return (Guid)ViewState["GameId"];
        }
        set { ViewState["GameId"] = value; }
    }

    public void Refresh()
    {
        using (IGameService service = ServiceFactory.GameService)
        {
            GameVideo video = service.GetAssociatedVideo(this.GameId);
            if (video != null)
            {
                uiTxtGameVideoTitle.Text = video.Text;
                uiTxtGameVideoUrl.Text = video.Link;
                uiTxtGameVideoDescription.Text = video.Text;
            }
            else
            {
                this.Clear();
            }
        }
    }

    public void Clear()
    {
        uiTxtGameVideoTitle.Text = String.Empty;
        uiTxtGameVideoUrl.Text = String.Empty;
        uiTxtGameVideoDescription.Text = String.Empty;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Refresh();
        }
    }

    protected void uiBtnSaveGameVideo_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            using (IGameAdminService adminService = ServiceFactory.GameAdminService)
            using (IGameService service = ServiceFactory.GameService)
            {
                GameVideo video = service.GetAssociatedVideo(this.GameId);
                if (null == video)
                {
                    video = new GameVideo();
                    video.GameId = this.GameId;
                }
                video.Title = uiTxtGameVideoTitle.Text;
                video.Link = uiTxtGameVideoUrl.Text;
                video.Text = uiTxtGameVideoDescription.Text;

                adminService.SetAssociatedVideo(video);
            }
        }
    }
    protected void uiBtnClearGameVideo_Click(object sender, EventArgs e)
    {
        using (IGameAdminService adminService = ServiceFactory.GameAdminService)
        {
            adminService.ClearAssociatedVideo(this.GameId);
        }
    }
}
